package team.seekers.catering.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import team.seekers.catering.common.enums.DRIsEnum;
import team.seekers.catering.common.utils.CateringUtil;
import team.seekers.catering.common.utils.ConvertUtil;
import team.seekers.catering.entity.dataobject.Food;
import team.seekers.catering.entity.dataobject.User;
import team.seekers.catering.entity.params.add.FoodAddPA;
import team.seekers.catering.entity.params.add.FoodContentListPA;
import team.seekers.catering.entity.params.add.FoodUpdatePA;
import team.seekers.catering.entity.temp.NutrimentListDTO;
import team.seekers.catering.entity.vo.info.FoodInfoVO;
import team.seekers.catering.entity.vo.list.FoodContentListVO;
import team.seekers.catering.entity.vo.list.FoodListVO;
import team.seekers.catering.entity.vo.list.NutrimentListVO;
import team.seekers.catering.mapper.FoodMapper;
import team.seekers.catering.service.FoodService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import team.seekers.catering.service.IngredientService;
import team.seekers.catering.service.UserService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 食物表 服务实现类
 * </p>
 *
 * @author hbc
 * @since 2022-02-04
 */
@Service
public class FoodServiceImpl extends ServiceImpl<FoodMapper, Food> implements FoodService {

    @Autowired
    private IngredientService ingredientService;

    @Autowired
    private UserService userService;

    @Override
    public Map<String, Object> adminPageOrder(long current, long limit, QueryWrapper<Food> wrapper) {
        Page<Food> page = new Page<>(current,limit);
        wrapper.orderByDesc("gmt_create");
        page(page,wrapper);
        Long total = page.getTotal();
        if(page.getRecords() == null){
            return null;
        }
        System.out.println("获取的记录是"+page.getRecords());

        List<FoodListVO> list = new ArrayList<>();
        for (Food record : page.getRecords()) {
            FoodListVO vo = new FoodListVO();
            BeanUtils.copyProperties(record,vo);
            list.add(vo);
        }
        Map<String,Object> map = new HashMap<>();
        map.put("total",total);
        map.put("rows",list);
        return map;
    }

    @Override
    public String add(FoodAddPA foodAddPA) {
        Food food = new Food();
        BeanUtils.copyProperties(foodAddPA,food);
        food.setContent(ConvertUtil.contentFromContentList(foodAddPA.getContentList()));
        food.setNd(ConvertUtil.ndFromContentList(foodAddPA.getNd()));
        save(food);
        return food.getId();
    }

    @Override
    public FoodInfoVO get(String id) {
        Food food = getById(id);
        FoodInfoVO foodInfoVO = new FoodInfoVO();
        BeanUtils.copyProperties(food,foodInfoVO);

        ArrayList<NutrimentListVO> nutrimentListVOS = new ArrayList<>();
        List<NutrimentListDTO> nutrimentListDTOS = ConvertUtil.ndToNutrimentList(food.getNd());
        for (NutrimentListDTO nutrimentListDTO : nutrimentListDTOS) {
            nutrimentListVOS.add(new NutrimentListVO(
                    DRIsEnum.getByValue(nutrimentListDTO.getNutrimentId()).getName(),
                    nutrimentListDTO.getAmount()+" "+DRIsEnum.getByValue(nutrimentListDTO.getNutrimentId()).getUnit().split("/")[0],
                    nutrimentListDTO.getNutrimentContent()
            ));
        }

        ArrayList<FoodContentListVO> foodContentListVOS = new ArrayList<>();
        List<FoodContentListPA> foodContentListPAS = ConvertUtil.contentToContentList(food.getContent());
        for (FoodContentListPA foodContentListPA : foodContentListPAS) {
            foodContentListVOS.add(new FoodContentListVO(
                    foodContentListPA.getId(),
                    ingredientService.getById(foodContentListPA.getId()).getNcName(),
                    foodContentListPA.getAmount()
            ));
        }
        foodInfoVO.setNd(nutrimentListVOS);
        foodInfoVO.setContentList(foodContentListVOS);
        return foodInfoVO;
    }

    @Override
    public String update(FoodUpdatePA foodUpdatePA) {
        Food food = new Food();
        BeanUtils.copyProperties(foodUpdatePA,food);
        food.setContent(ConvertUtil.contentFromContentList(foodUpdatePA.getContentList()));
        food.setNd(ConvertUtil.ndFromContentList(foodUpdatePA.getNd()));
        updateById(food);
        return food.getId();
    }

    @Override
    public FoodInfoVO todayPush() {
        String userId = CateringUtil.getUserId();
        User user = userService.getById(userId);
        Food food = lambdaQuery().eq(Food::getType, user.getFoodType())
                .eq(Food::getIsToday, 1).one();
        return this.get(food.getId());
    }

    @Override
    public boolean isExist(String foodId) {
        return getById(foodId) != null;
    }
}
